package api

import (
	"context"
	"gitee.com/leo-xin/novel_distribution_sdk_go/yuewen/config"
	"net/http"
	"net/url"
)

type GetAppListApiService service

type GetAppListRequest struct {
	ctx        context.Context
	ApiService *GetAppListApiService
	startTime  *uint64
	endTime    *uint64
	page       *uint64
}

type GetAppListResponse struct {
	Code    *int64       `json:"code,omitempty"`
	Message *string      `json:"message,omitempty"`
	Data    *AppListData `json:"data,omitempty"`
}

type AppListData struct {
	Page       *uint64        `json:"page,omitempty"`
	TotalCount *uint64        `json:"total_count,omitempty"`
	List       []*AppListItem `json:"list,omitempty"`
}

type AppListItem struct {
	AppName *string `json:"app_name,omitempty"`
	AppFlag *string `json:"appflag,omitempty"`
}

func (r *GetAppListRequest) Page(page uint64) *GetAppListRequest {
	r.page = &page
	return r
}

func (r *GetAppListRequest) StartTime(startTime uint64) *GetAppListRequest {
	r.startTime = &startTime
	return r
}

func (r *GetAppListRequest) EndTime(endTime uint64) *GetAppListRequest {
	r.endTime = &endTime
	return r
}

func (r *GetAppListRequest) Execute() (*GetAppListResponse, *http.Response, error) {
	return r.ApiService.getExecute(r)
}

func (r *GetAppListRequest) ApiSecret(ApiSecret string) *GetAppListRequest {
	r.ctx = context.WithValue(r.ctx, config.ApiSecret, ApiSecret)
	return r
}

func (r *GetAppListRequest) Email(Email string) *GetAppListRequest {
	r.ctx = context.WithValue(r.ctx, config.Email, Email)
	return r
}

func (r *GetAppListRequest) WithLog(enable bool) *GetAppListRequest {
	if enable {
		r.ctx = context.WithValue(r.ctx, config.ContextEnableLog, true)
	}
	return r
}

func (a *GetAppListApiService) Get(ctx context.Context) *GetAppListRequest {
	return &GetAppListRequest{
		ApiService: a,
		ctx:        ctx,
	}
}

func (a *GetAppListApiService) getExecute(r *GetAppListRequest) (*GetAppListResponse, *http.Response, error) {

	cfg := a.client.Cfg
	var (
		localVarPath         = cfg.GetBasePath() + "/" + cfg.Prefix + "/GetAppList"
		localVarHTTPMethod   = http.MethodGet
		localVarPostBody     map[string]string
		localVarFormParams   = url.Values{}
		localVarHeaderParams = make(map[string]string)
		localVarQueryParams  = url.Values{}
		localVarReturnValue  *GetAppListResponse
	)

	r.ctx = a.client.prepareCtx(r.ctx)

	if r.page != nil {
		localVarQueryParams.Add("page", toString(r.page))
	}
	if r.startTime != nil {
		localVarQueryParams.Add("start_time", toString(r.startTime))
	}
	if r.endTime != nil {
		localVarQueryParams.Add("end_time", toString(r.endTime))
	}

	req, err := a.client.buildRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams)
	if err != nil {
		return localVarReturnValue, nil, err
	}

	localVarHTTPResponse, err := a.client.call(r.ctx, req, &localVarReturnValue)
	if err != nil || localVarHTTPResponse == nil {
		return localVarReturnValue, localVarHTTPResponse, err
	}
	return localVarReturnValue, localVarHTTPResponse, nil
}
